/* =================================================
 * Package:			dk.iha.toksEditor
 * File:			LogIn.java
 * Description:		This is the activity for which
 * 					is responsible for logging in
 * 					the user.
 * 
 * Responsible
 * =================================================
 * Name:			Vinnie Juul
 * Study number:	09767
 */

package dk.iha.toksEditor;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

public class LogIn extends Activity {
	private User user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.login_view);
		user = new User(this);
		
		EditText pass = (EditText)findViewById(R.id.passwordEditText);
		pass.setOnKeyListener(new OnKeyListener() {
        	public boolean onKey(View v, int keyCode, KeyEvent event) {
        		if(event.getAction() == KeyEvent.ACTION_UP)
        			if(keyCode == KeyEvent.KEYCODE_ENTER){
        				logIn((View)findViewById(R.id.loginButton));
        				return true;
        			}
        		return false;
        	}
        });
	}
	
	@Override
	protected void onResume() {		
		EditText userNameEditText = (EditText)findViewById(R.id.userNameEditText);
		userNameEditText.setText(user.getUserName().toString());
		
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		if(prefs.getBoolean("checkboxPref", false)){
			getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		}else{
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		}
		
		super.onResume();
	}
	
	/**
	 * Check the data and login if the user is in database
	 * @param view
	 */
	public void logIn(View view)
	{
		EditText name = (EditText)findViewById(R.id.userNameEditText);
		EditText pass = (EditText)findViewById(R.id.passwordEditText);

		String[] parameters = {name.getText().toString(),pass.getText().toString()};
		
		AsyncLogin al = new AsyncLogin();
		al.execute(parameters);
	}
	
	/**
	 * Use to run function calls asynchronous
	 * @author Vinnie Juul
	 *
	 */
	private class AsyncLogin extends AsyncTask<String, Integer, Boolean>
	{
		private boolean succeded = false;
		private ProgressDialog mpd;

		@Override
		protected void onPreExecute() {
			mpd = ProgressDialog.show(LogIn.this, getString(R.string.LogInProgressDialogTitle), getString(R.string.LogInProgressDialogDescription), true, false);
			super.onPreExecute();
		}
		
		@Override
		protected Boolean doInBackground(String... params) {
			succeded = user.LogIn(params[0], params[1]);
			return succeded;
		}
		
		@Override
		protected void onPostExecute(Boolean result) {
			mpd.dismiss();
			
			if(succeded){
				setResult(RESULT_OK, new Intent());
				finish();
			}else{
				Toast mToast = Toast.makeText(getApplicationContext(), R.string.LogInWrongUserOrPass, Toast.LENGTH_SHORT);
				mToast.setGravity(Gravity.CENTER, 0, 0);
				mToast.show();
			}
			
			super.onPostExecute(result);
		}
	}
}
